import {
  AmbientLight,
  AxesHelper,
  DirectionalLight,
  GridHelper,
  Group,
  PerspectiveCamera,
  Scene,
  WebGLRenderer,
} from "three";
import { OrbitControls } from "three/examples/jsm/Addons.js";

export default class Y3DEngine {
  constructor() {
    this.init();
  }
  init() {
    this.initScene();
    this.initCamera();
    this.initRenderer();
    this.initControls();
    this.initLights();
    this.initHelpers();
    this.animate();
  }

  animate = () => {
    requestAnimationFrame(this.animate);
    this.renderer.render(this.scene, this.camera);
  };

  initScene() {
    this.scene = new Scene();
  }

  initLights() {
    this.lights = new Group();
    this.scene.add(this.lights);
    const ambientLight = new AmbientLight(0xffffff, 1);
    this.lights.add(ambientLight);

    const directionalLight = new DirectionalLight(0xffffff, 1);
    directionalLight.position.set(0, 1, 0);
    this.lights.add(directionalLight);
  }

  initHelpers() {
    this.helpers = new Group();
    this.scene.add(this.helpers);
    const gridHelper = new GridHelper(100, 11);
    this.helpers.add(gridHelper);

    const axisHelper = new AxesHelper(10);
    this.helpers.add(axisHelper);
  }

  initRenderer() {
    const { width, height } = this;
    const renderer = new WebGLRenderer({ antialias: true });
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize(width, height);
    document.body.appendChild(renderer.domElement);
    this.renderer = renderer;
  }

  initCamera() {
    const camera = new PerspectiveCamera(75, this.aspect, 0.1, 1000);
    camera.position.z = 3;
    this.camera = camera;
  }

  get aspect() {
    return this.width / this.height;
  }

  get width() {
    return window.innerWidth;
  }

  get height() {
    return window.innerHeight;
  }

  initControls() {
    const controls = new OrbitControls(this.camera, this.renderer.domElement);
  }
}

window.addEventListener("DOMContentLoaded", () => {
  const engine = new Y3DEngine();
});
